
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "RawCAD.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWndEx)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};
static UINT PropertyButtons[] = 
{
	//	ID_SETTING_GRID,
	//	ID_SETTING_ORTH,
	ID_PROPERTY,	//0
	ID_SEPARATOR,	//1
	ID_SEPARATOR,	//2:LINESTYLE
	ID_SEPARATOR,	//3
	ID_SEPARATOR,	//4:LINECOLOR
	ID_SEPARATOR,	//5
	ID_SEPARATOR,	//6:LINEWIDTH
};
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);


	//////////////////////////////////////////////////////////////////////////
	//m_wndModifyBar
	if(!m_wndModifyBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndModifyBar.LoadToolBar(IDR_TOOLBAR_MODIFY))
	{
		TRACE0("Failed to create modify toolbar\n");
		return -1;      // fail to create
	}
	m_wndModifyBar.SetWindowText(_T("modify toolbar!"));
	//////////////////////////////////////////////////////////////////////////
	//m_wndCreateBar
	if(!m_wndCreateBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_LEFT
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndCreateBar.LoadToolBar(IDR_TOOLBAR_DRAW_TYPE))
	{
		TRACE0("Failed to create modify toolbar\n");
		return -1;      // fail to create
	}

	m_wndCreateBar.SetWindowText(_T("draw toolbar"));
	//////////////////////////////////////////////////////////////////////////
	//m_wndViewBar
	if(!m_wndViewBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_LEFT
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndViewBar.LoadToolBar(IDR_TOOLBAR_VIEW))
	{
		TRACE0("Failed to create modify toolbar\n");
		return -1;      // fail to create
	}
	m_wndViewBar.SetWindowText(_T("view toolbar"));
    //////////////////////////////////////////////////////////////////////////
	//m_wndPropBar
	if(!CreatePropertyBar())
		return -1;      // fail to create
	m_wndPropBar.SetWindowText(_T("property toolbar")) ;
	//////////////////////////////////////////////////////////////////////////
	m_wndCreateBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndViewBar.EnableDocking(CBRS_ALIGN_ANY) ;
	m_wndModifyBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndPropBar.EnableDocking(CBRS_ALIGN_ANY) ;
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndModifyBar);
	//////////important !!!!!!!
	DockControlBarLeftOf(&m_wndCreateBar, &m_wndToolBar);
	DockControlBarLeftOf(&m_wndPropBar, &m_wndModifyBar);
	DockControlBarLeftOf(&m_wndViewBar, &m_wndPropBar) ;

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}
#endif //_DEBUG


// CMainFrame message handlers

BOOL CMainFrame::CreatePropertyBar()
{
	const int nDropHeight = 100 ;
	if(!m_wndPropBar.CreateEx(this,TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE|CBRS_LEFT|
		CBRS_SIZE_DYNAMIC|CBRS_TOOLTIPS|CBRS_FLYBY, CRect(1,1,1,1),IDR_TOOLBAR_PROPERTY)||
		!m_wndPropBar.LoadBitmap(IDR_TOOLBAR_PROPERTY)||
		!m_wndPropBar.SetButtons(PropertyButtons,sizeof(PropertyButtons)/sizeof(UINT)))	
	{
		TRACE0("Failed to create propertybar\n") ;
		return FALSE ;
	}
	//set IDW_LINESTYLE
	m_wndPropBar.SetButtonInfo(1,ID_SEPARATOR, TBBS_SEPARATOR, 12) ;
	m_wndPropBar.SetButtonInfo(2,IDW_LINESTYLE , TBBS_SEPARATOR, 150) ;

	m_wndPropBar.SetButtonInfo(3,ID_SEPARATOR, TBBS_SEPARATOR, 12) ;
	m_wndPropBar.SetButtonInfo(4,IDW_COLOR, TBBS_SEPARATOR,100) ;

	m_wndPropBar.SetButtonInfo(5,ID_SEPARATOR, TBBS_SEPARATOR, 12) ;
	m_wndPropBar.SetButtonInfo(6,IDW_LINEWIDTH , TBBS_SEPARATOR, 150) ;

	CRect rc ;
//////////////////////////////////////////////////////////////////////////
	//Draw the LineType combobox 
	//get the combobox rect
	m_wndPropBar.GetItemRect(2,&rc) ;
	rc.bottom = rc.top + nDropHeight;
	if(!m_wndPropBar.m_wndLineStyleCmb.Create(
		CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP|CBS_AUTOHSCROLL|
		WS_VSCROLL|CBS_OWNERDRAWVARIABLE,
		rc, &m_wndPropBar, IDW_LINESTYLE))
	{
		TRACE0("Failed to create linestyle combo--box!") ;
		return FALSE ;
	}
	m_wndPropBar.m_wndLineStyleCmb.ShowWindow(SW_SHOW);
	//Add item to combobox
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_SOLID, _T("Solid")) ;
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASH, _T("Dash")) ;
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DOT, _T("Dot")) ;
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASHDOT, _T("DashDot")) ;
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASHDOTDOT, _T("DashDotDot")) ;
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_NULL, _T("Null")) ;
	m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_INSIDEFRAME, _T("InsideFrame")) ;
	//default selection
	m_wndPropBar.m_wndLineStyleCmb.SetCurSel(0) ;
	m_wndPropBar.m_wndLineStyleCmb.ShowWindow(SW_SHOW);

//////////////////////////////////////////////////////////////////////////
	//color combobox
	m_wndPropBar.GetItemRect(4, &rc) ;
	rc.bottom = rc.top + nDropHeight ;

	if(!m_wndPropBar.m_wndColorCmb.Create(	CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP|
		WS_VSCROLL|CBS_OWNERDRAWVARIABLE,
		rc, &m_wndPropBar,IDW_COLOR))
	{
		TRACE0("Failed to create combo--box!\n") ;
		return FALSE ;
	}

	m_wndPropBar.m_wndColorCmb.AddItem(RGB(255,0,0),_T("red")) ;
	m_wndPropBar.m_wndColorCmb.AddItem(RGB(0,255,0),_T("Green")) ;
	m_wndPropBar.m_wndColorCmb.AddItem(RGB(0,0,255),_T("Blue")) ;
	m_wndPropBar.m_wndColorCmb.AddItem(RGB(255,255,0),_T("Yellow")) ;
	m_wndPropBar.m_wndColorCmb.AddItem(RGB(0,255,255),_T("LightBlue")) ;
	m_wndPropBar.m_wndColorCmb.AddItem(RGB(255,0,255),_T("Cyan"));
	m_wndPropBar.m_wndColorCmb.AddItem(RGB(0,0,0),_T("Black")) ;

	m_wndPropBar.m_wndColorCmb.SetCurSel(6) ;

//////////////////////////////////////////////////////////////////////////
	//line width combobox
	m_wndPropBar.GetItemRect(6,&rc) ;
	rc.bottom = rc.top + nDropHeight ;

	if(!m_wndPropBar.m_wndLineWidthCmb.Create(
		CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP|
		WS_VSCROLL|CBS_OWNERDRAWVARIABLE,
		rc, &m_wndPropBar, IDW_LINEWIDTH))
	{
		TRACE0("Failed to create lineWidth combo--box!") ;
		return FALSE ;
	}
	m_wndPropBar.m_wndLineWidthCmb.AddItem(1, _T("Line Width1")) ;
	m_wndPropBar.m_wndLineWidthCmb.AddItem(2, _T("Line Width2")) ;
	m_wndPropBar.m_wndLineWidthCmb.AddItem(3, _T("Line Width3")) ;
	m_wndPropBar.m_wndLineWidthCmb.AddItem(4, _T("Line Width4")) ;

	m_wndPropBar.m_wndLineWidthCmb.SetCurSel(0) ;

	return TRUE ;
}

void CMainFrame::DockControlBarLeftOf( CControlBar* Bar,CControlBar* LeftOf )
{
	CRect rect;
	DWORD dw;
	UINT n;

	// get MFC to adjust the dimensions of all docked ToolBars
	// so that GetWindowRect will be accurate
	RecalcLayout();
	LeftOf->GetWindowRect(&rect);
	rect.OffsetRect(1,0);
	dw=LeftOf->GetBarStyle();
	n = 0;
	n = (dw&CBRS_ALIGN_TOP) ? AFX_IDW_DOCKBAR_TOP : n;
	n = (dw&CBRS_ALIGN_BOTTOM && n==0) ? AFX_IDW_DOCKBAR_BOTTOM : n;
	n = (dw&CBRS_ALIGN_LEFT && n==0) ? AFX_IDW_DOCKBAR_LEFT : n;
	n = (dw&CBRS_ALIGN_RIGHT && n==0) ? AFX_IDW_DOCKBAR_RIGHT : n;

	// When we take the default parameters on rect, DockControlBar will dock
	// each Toolbar on a separate line.  By calculating a rectangle, we in effect
	// are simulating a Toolbar being dragged to that location and docked.
	DockControlBar(Bar,n,&rect);
}
